package com.ruileroi.ai.controller;

import com.ruileroi.ai.entity.vo.MessageVO;
import com.ruileroi.ai.repository.ChatHistoryRepository;
import com.ruileroi.ai.service.ISpringAiChatMemoryService;
import com.ruileroi.ai.service.ISpringAiChatRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: ruileroi-ai
 * @description:
 * @author: wangrui
 * @create: 2025-04-24 22:18
 **/

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    private final ChatHistoryRepository inMemoryChatHistoryRepository;
    private final ChatMemory inMemoryChatMemory;

    private final ISpringAiChatMemoryService chatMemoryService;
    private final ISpringAiChatRecordService recordService;

    /**
     * 查询会话历史列表
     * @param type 业务类型，如：chat,service,pdf
     * @return chatId列表
     */
    @GetMapping("/{type}")
    public List<String> getChatIds(@PathVariable("type") String type) {
//        return inMemoryChatHistoryRepository.getChatIds(type);
        return recordService.findConversationIds(type);
    }

    /**
     * 根据业务类型、chatId查询会话历史
     * @param type 业务类型，如：chat,service,pdf
     * @param chatId 会话id
     * @return 指定会话的历史消息
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId) {
//        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
//        List<Message> messages = inMemoryChatMemory.get(chatId);
//        if(messages == null) {
//            return List.of();
//        }
//        return messages.stream().map(MessageVO::new).toList();
        return chatMemoryService.findByConversationId(chatId).stream().map(MessageVO::new).toList();
    }

}
